# -*- coding: utf-8 -*-
import os
import socket
import struct

def get_ip_address(ifname):
    # 获取本地ip
    hostip = socket.gethostbyname(socket.gethostname())
    return hostip

def get_hostname():
    # 获取本机主机名
    hostname = socket.gethostname()
    return hostname

# 传说中使用如下方法获得IP
#    if os_name == 'nt':
#        hostip = socket.gethostbyname(socket.gethostname())
#    else:
#        try:
#            import fcntl
#            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#            hostip = socket.inet_ntoa(fcntl.ioctl(sock.fileno(), 0x8915,\
#                    struct.pack('256s', ifname[:15]))[20:24])
#        except:
#            hostip = socket.gethostbyname(socket.gethostname())

# 传说中使用如下方法获得主机名:
#    if os_name == 'nt':
#        hostname = os.getenv("computername")
#    else:
#        host = os.popen("echo $HOSTNAME")
#        try:
#            hostname = host.read()
#        finally:
#            host.close()